package com.laopan.elasticsearch.core.geo;

import com.laopan.elasticsearch.bean.geo.Location;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.GeoDistanceSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;

/**
 * 地理条件构造器
 *
 * @author laopan
 * @className GeoBuilder
 * @date 2021/4/20 11:34
 */

public class GeoBuilder {

    public static void geoSortBuilder(String fieldName, Location location, SearchSourceBuilder builder) {
        if (null != location) {
            GeoDistanceSortBuilder geoDistanceSortBuilder = location.geoSortBuilder(fieldName);
            //先根据搜索分值排序，加入该分值排序之后，返回的分值数据不是距离
            builder.sort(SortBuilders.scoreSort());
            if (null != geoDistanceSortBuilder) {
                builder.sort(geoDistanceSortBuilder);
            }
        }
    }
}
